<?php
declare (strict_types=1);

namespace app\sysuser\controller\core;

use app\common\controller\SysBackend;
use app\common\model\banxi\core\CoreAccountClassHour as CoreAccountClassHourModel;
use app\common\service\organization\OrganizationLevelService;
use app\common\service\organization\OrganizationRewardService;
use app\common\service\student\AccountClassHourService;
use ba\Exception;
use think\db\exception\DbException;
use think\db\exception\PDOException;
use think\exception\ValidateException;
use think\facade\Db;

class CoreAccountClassHour extends SysBackend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new CoreAccountClassHourModel();
    }


    /**
     * 分页查询
     * @return void
     * @throws DbException
     */
    public function listPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->getListPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $param = $this->request->param(["query" => []]);
        if (empty($param['query'])) {
            $this->fail('请传递你要查询的记录');
        }
        $where = get_query($param);
        $info = $this->model->getInfo($where);
        if (empty($info)) {
            $this->fail('记录不存在');
        }
        $this->ok('操作成功', $info);
    }


    //课时扣除机构
    public function deductOrgan()
    {
        $param = $this->request->param(['organizationId', 'classHourType', 'classHourNum', 'memo' => '', 'password']);
        try {
            $admin = $this->auth->getAdmin();
            $isError = verifyMd5Password($param['password'], $admin['password'], $admin['salt']);
            if (!$isError) {
                throw new ValidateException('密码错误');
            }
            $accountClassHourService = new AccountClassHourService();
            $res = $accountClassHourService->deductOrgan($param);
            if (!$res) {
                throw new ValidateException($accountClassHourService->getError());
            }
        } catch (Exception|PDOException|ValidateException $e) {
            $this->fail($e->getMessage());
        }
        $this->ok('操作成功');
    }

    /**
     * 课时扣除学员
     */
    public function deductStudent()
    {
        $param = $this->request->param(['classHourNum', 'classHourType', 'memo' => '', 'password', 'studentId']);
        try {
            $admin = $this->auth->getAdmin();
            $isError = verifyMd5Password($param['password'], $admin['password'], $admin['salt']);
            if (!$isError) {
                throw new ValidateException('密码错误');
            }
            $accountClassHourService = new AccountClassHourService();
            $res = $accountClassHourService->deductStudent($param);
            if (!$res) {
                throw new ValidateException($accountClassHourService->getError());
            }
        } catch (Exception|PDOException|ValidateException $e) {
            $this->fail($e->getMessage());
        }
        $this->ok('操作成功');
    }

    //充值机构
    public function rechargeOrgan()
    {
        $param = $this->request->param(['organizationId', 'classHourType', 'classHourNum', 'rechargeType', 'payAmount', 'payType', 'password']);
        Db::startTrans();
        try {
            $admin = $this->auth->getAdmin();
            $isError = verifyMd5Password($param['password'], $admin['password'], $admin['salt']);
            if (!$isError) {
                throw new ValidateException('密码错误');
            }
            $accountClassHourService = new AccountClassHourService();
            $res = $accountClassHourService->rechargeOrgan(false, $param);
            if (!$res) {
                throw new ValidateException($accountClassHourService->getError());
            }
            // //新校区陪跑服务奖励
            $organizationRewardService = new OrganizationRewardService();
            $organizationRewardService->accompanyServiceReward($param);
            // //校区升级
            $organizationRewardService = new OrganizationLevelService();
            $organizationRewardService->campusUpgrade($param['organizationId']);
            Db::commit();
        } catch (Exception|PDOException|ValidateException $e) {
            Db::rollback();
            $this->fail($e->getMessage());
        }
        $this->ok('操作成功');

    }

    /**
     * 充值学员
     */
    public function rechargeStudent()
    {
        $param = $this->request->param(['studentId', 'name', 'classHourType', 'payType', 'payAmount', 'classHourNum', 'password']);
        try {
            $admin = $this->auth->getAdmin();
            $isError = verifyMd5Password($param['password'], $admin['password'], $admin['salt']);
            if (!$isError) {
                throw new ValidateException('密码错误');
            }
            $accountClassHourService = new AccountClassHourService();
            $res = $accountClassHourService->rechargeStudent($param);
            if (!$res) {
                throw new ValidateException($accountClassHourService->getError());
            }
            //合伙人升级
            $organizationLevelService = new OrganizationLevelService();
            $organizationLevelService->partnerUpgrade($param['studentId']);
        } catch (Exception|PDOException|ValidateException $e) {
            $this->fail($e->getMessage());
        }
        $this->ok('操作成功');

    }

    //退费机构
    public function refundOrgan()
    {
        $param = $this->request->param(['organizationId', 'classHourType', 'classHourNum', 'memo', 'password']);
        try {
            $admin = $this->auth->getAdmin();
            $isError = verifyMd5Password($param['password'], $admin['password'], $admin['salt']);
            if (!$isError) {
                throw new ValidateException('密码错误');
            }
            $accountClassHourService = new AccountClassHourService();
            $res = $accountClassHourService->refundOrgan(false, $param);
            if (!$res) {
                throw new ValidateException($accountClassHourService->getError());
            }
        } catch (Exception|PDOException|ValidateException $e) {
            $this->fail($e->getMessage());
        }
        $this->ok('操作成功');

    }
}
